﻿using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using ProcessBase.Models;
using SubstrateHelper.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SubstrateHelper.Helpers
{
    public class ProcessDetailHelper
    {
        public static List<ProcessBase.Models.ProcessDetail> GetDetails(string guid)
        {

            List<ProcessDetail> details = new List<ProcessDetail>();
            using (var scope = Global.Host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var Db = services.GetRequiredService<SubstrateHelperDbContext>();


                    var records = Db.Records.Where(x => x.Guid == guid);
                    var process_name = records.Select(x => x.ProcessName).FirstOrDefault();
                    var record_title = Db.ProcessRecordFileCodes.Where(x => x.ProcessName == process_name).Select(x => x.RecordFileCode).FirstOrDefault();
                    
                    foreach (var record in records)
                    {
                        ProcessDetail detail = new ProcessDetail();

                        var para = Db.Parameters.Where(x => x.ParameterName == record.ParameterName && x.ProcessName == record.ProcessName).FirstOrDefault();

                        detail.Parameter = para;
                        detail.Record = record;
                        detail.RecordTitle = record_title;

                        details.Add(detail);
                    }

                    details = details.OrderBy(x => x.Parameter.OrderID).ToList();
                    return details;

                }
                catch (Exception ex)
                {
                    
                    var logger = services.GetRequiredService<ILogger<Program>>();
                    logger.LogError(ex, "MakeStatistics");

                    return details;
                }
            }
        }

        //public static List<ProcessBase.Models.ProcessDetail> NewDetails(string mainLotNo,string process)
        public static List<ProcessBase.Models.ProcessDetail> NewDetails(string process)
        {
            List<ProcessDetail> details = new List<ProcessDetail>();
            string guid = System.Guid.NewGuid().ToString();
            using (var scope = Global.Host.Services.CreateScope())
            {
                var services = scope.ServiceProvider;
                try
                {
                    var Db = services.GetRequiredService<SubstrateHelperDbContext>();

                    if (string.IsNullOrWhiteSpace(process))
                    {                        
                        return null;
                    }

                    

                    DateTime date = DateTime.Now.Date;                    

                    //所有参数
                    var paras = Db.Parameters.Where(x => x.ProcessName == process);
                    foreach (var para in paras)
                    {
                        ProcessBase.Models.ProcessDetail detail = new ProcessDetail();
                        detail.Parameter = para;

                        Record record = new Record();
                        
                        record.Guid = guid;
                        record.Date = date;
                        record.ProcessName = para.ProcessName;
                        record.ParameterName = para.ParameterName;
                        if (para.ParameterName == "主批号")
                        {
                            //record.StringValue = mainLotNo;
                            record.StringValue = "";
                        }
                        else if (para.ParameterName == "产出标识")
                        {
                            //var shortName = Db.ProcessShortNames.Where(x => x.ProcessName == para.ProcessName).Select(x => x.ShortName).FirstOrDefault();
                            //record.StringValue = $"{mainLotNo}-{shortName}-";
                            record.StringValue = "";
                        }
                        else
                        {
                            record.StringValue = para.DefaultStringValue;
                        }
                        record.NumberValue = para.DefaultNumberValue;
                        record.DateTimeValue = para.DefaultDateTimeValue;
                        detail.Record = record;

                        //
                        details.Add(detail);
                    }

                    details = details.OrderBy(x => x.Parameter.OrderID).ToList();

                    return details;

                }
                catch (Exception ex)
                {

                    var logger = services.GetRequiredService<ILogger<Program>>();
                    logger.LogError(ex, "MakeStatistics");

                    return details;
                }
            }
        }
    }
}
